package com.vworks.framework.base;

import javax.servlet.http.HttpServletRequest;

import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public abstract class FormController extends MultiActionController{

	
	protected <T>  BindingResult getValidatorResult(HttpServletRequest request,T validatorObject)  throws Exception{
		
		ServletRequestDataBinder servletRequestDataBinder = createBinder(request, validatorObject);
		
		servletRequestDataBinder.bind(request);
		
		BindingResult result=servletRequestDataBinder.getBindingResult();
		
		new Validator(){

			@Override public boolean supports(Class<?> arg0) {
				
				return supportsForm(arg0);
			}

			@Override public void validate(Object arg0, Errors arg1) {
				
				validateForm(arg0, arg1);
			}

		}.validate(validatorObject, result);
		
		return result;
	}
	
	protected abstract boolean supportsForm(Class<?> entity);
	
	protected abstract void validateForm(Object entity,Errors errors);
	
}
